# -*- coding: utf-8 -*-
from re_utils import re_anonymize_need,scan_files, exts, dir_path
import time

def scan_file(file_path):
    time1 = time.time()
    bool_flag = False
    with open(file_path, 'r', encoding='utf-8') as infile:
        for line in infile:
            re_text = re_anonymize_need(line)
            if re_text != line:
                print("原文信息: ", line)
                print("存在敏感信息: ", re_text)
                bool_flag = True
                break
    if bool_flag is not True:
        print("扫描完毕，不存在敏感信息，耗时 ", (time.time() - time1))
    return bool_flag


if __name__ == '__main__':

    # 备份日志的后缀
    extensions = [ext.strip() for ext in exts.split(',')] if exts else None
    print(extensions)
    try:
        all_count = 0
        bool_flag_count = 0
        start_time = time.time()
        for idx, file in enumerate(scan_files(dir_path, extensions, max_depth=5), 1):
            print(f"{idx:>4}. {file}")
            all_count = all_count + 1
            try:
                bool_flag_result = scan_file(file)
                if bool_flag_result:
                    bool_flag_count = bool_flag_count + 1
            except Exception as e:
                print(f'失败错误: {e}')
        print(f"总共扫描{all_count}个文件， 敏感信息文件为: {bool_flag_count}个，耗时: {(time.time() - start_time)} ")
    except Exception as e:
        print(f'错误: {e}')
